package com.furongji.education.mq;

import com.furongji.education.config.RabbitConfiguration;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class RabbitOrderProducer {

    private final RabbitTemplate rabbitTemplate;

    private static final int ORDER_TIMEOUT_MILLIS = 1000 * 60;

    public void sendOrderDelayMessage(String orderId) {
        // 设置消息TTL为30分钟
        rabbitTemplate.convertAndSend(
                RabbitConfiguration.ORDER_DELAY_EXCHANGE,
                RabbitConfiguration.ORDER_DELAY_ROUTING_KEY,
                orderId,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration(String.valueOf(ORDER_TIMEOUT_MILLIS));
                        return message;
                    }
                }
        );
    }

}
